// 引入路由和vuex

import router from './router'
import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration
const whiteList = ['/login', '/404'] // 白名单

router.beforeEach(async(to, from, next) => {
  NProgress.start()
  // 获取token
  const token = store.state.user.token

  if (token) {
    if (to.path === '/login') {
    //   NProgress.done()    //这里不阻止进度条的原因是有后置守卫
      next('/')
    } else {
      NProgress.done()

      // 获取用户信息
      await store.dispatch('user/userInfo')
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      NProgress.done()
      next()
    } else {
    //   NProgress.done()      //这里不阻止进度条的原因是有后置守卫
      next('/login')
    }
  }
})

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
